﻿package  {
	
	import flash.display.MovieClip;
	import flash.events.Event;
	
	
	public class Grapple extends MobObj {
		
		
		private var player:Player;
		private var willRemove:Boolean;
		
		public function Grapple(aX:Number,aY:Number,p:Player) {
			super(0,0,width,height);
			player = p;
			willRemove = false;
			this.addEventListener(Event.ADDED_TO_STAGE,onAdded);
		}
		
		public override function Update()
		{
			graphics.lineStyle(2,0x000000);
			graphics.clear()
			VelX = 5*Math.cos(rotation*Math.PI/180);// - player.VelX;
			VelY = 5*Math.sin(rotation*Math.PI/180);// - player.VelY;
			
			
			//THIS LINE WAS THE FIRGGING PROBLEM!!!!
			//graphics.lineTo(x,y);
			
			
			
			super.Update();
			if(willRemove){
				parent.removeChild(this);
			}
			
		}
		
		protected override function CollisionX():Boolean
		{
			var go:Boolean = true;
			var nextX:int;
			nextX = x;
			
			nextX += VelX;
							
			if(VelX > 0){
				nextX += width;
			}
			
			for(var i:int = 0; i < _tiles.length; i++){
					if(_tiles[i].CollisionCheck(nextX, this.y))
					{
						player.x = x;
						player.y = y;
						player.Grappling = false;
						willRemove = true;
						go = false;
					}
					
					if(_tiles[i].CollisionCheck(nextX, this.y + height))
					{
						player.x = x;
						player.y = y;
						player.Grappling = false;
						willRemove = true;
						go = false;
					}
			}
			return go;
			
		}
		
		protected override function CollisionY():Boolean
		{
			var go:Boolean = true;
			var nextY:int;
			
			nextY = y;
			nextY += VelY;
				
			if(VelY > 0){
				nextY += height;
			}
			for(var i:int = 0; i < _tiles.length; i++){
					if(_tiles[i].CollisionCheck(this.x, nextY))
					{
						player.x = x;
						player.y = y;
						player.Grappling = false;
						willRemove = true;
						go = false;
					}
					if(_tiles[i].CollisionCheck(this.x + width, nextY))
					{
						player.x = x;
						player.y = y;
						player.Grappling = false;
						willRemove = true;
						go = false;
					}
			}
			return go;
		}
		
		private function onAdded(e:Event){
			willRemove = false;
			graphics.moveTo(player.x, player.y);
			_tiles = player.tiles;
		}
		
	}
	
}
